<template>
  <div class="login">
    <img src="../assets/logo.png" alt="" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请设置用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请设置密码' }]"
      />
      <van-field
        v-model="repass"
        name="password"
        type="password"
        label="确认密码"
        placeholder="用户名"
        :rules="[{ required: true, message: '请再次填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <router-link class="reg" :to="{ name: 'Login' }"
      >已有账号？去登录>></router-link
    >
  </div>
</template>

<script>
import { reg } from "../services/user.js";
import { Toast } from "vant";
export default {
  data() {
    return {
      username: "",
      password: "",
      repass: "",
    };
  },
  methods: {
    onSubmit(values) {
      if (this.password == this.repass) {
        reg(values).then((res) => {
          {
            if (res.code === 1) {
              Toast("注册成功");
              this.$router.push({
                name: "Login",
              });
            } else {
              Toast(res.data);
            }
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
img {
  display: block;
  margin: 40px auto;
}
.reg {
  float: right;
  margin-right: 10px;
}
</style>